package com.ruoyi.web.controller.laiqianba.product;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.channel.enums.ChannelTypeEnum;
import com.ruoyi.channel.enums.PageTypeEnum;
import com.ruoyi.channel.service.IChannelBaseInfoService;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.db.model.ProductOnlieInfoParam;
import com.ruoyi.laiqianba.enums.ChargeTypeEnum;
import com.ruoyi.laiqianba.service.IApplicationInfoService;
import com.ruoyi.product.service.IProductBaseInfoService;
import com.ruoyi.product.service.enums.WarehouseTypeEnum;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.db.domain.ProductOnlieInfo;
import com.ruoyi.product.service.IProductOnlieInfoService;
import com.ruoyi.web.wrapper.ProductOnlieInfoWrapper;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 产品上线信息Controller
 * 
 * @author ruoyi
 * @date 2023-09-04
 */
@RestController
@RequestMapping("/product/onlieInfo")
public class ProductOnlieInfoController extends BaseController
{
    @Autowired
    private IProductOnlieInfoService productOnlieInfoService;
    @Autowired
    private IProductBaseInfoService productBaseInfoService;
    @Autowired
    private IChannelBaseInfoService channelBaseInfoService;
    @Autowired
    IApplicationInfoService  applicationInfoService;

    /**
     * 修改渠道基本信息
     * /channel/baseInfo/extData
     */
    @GetMapping("/extData")
    public AjaxResult extData(@RequestParam (value = "productId",required = false) Long productId)
    {
        JSONObject returnData = new JSONObject();
        returnData.put("pageTypeMapList", PageTypeEnum.getPageTypeMapList());
        returnData.put("chargeTypeList", ChargeTypeEnum.getChannelMapList());
        returnData.put("channelTypeList", ChannelTypeEnum.getChannelTypeMapList());
        returnData.put("channelNameList", channelBaseInfoService.getChannelNameList1());
        returnData.put("sceneMapList", WarehouseTypeEnum.getSceneMapList());
        returnData.put("appCodeList", applicationInfoService.getAppInfoMapList());
        //获取产品名称(搜索)
        returnData.put("productNameList", productBaseInfoService.productNameList());

        Long userId = null;
        if (!(SecurityUtils.loginUserIsAdmin())){
            // 当前登陆用户不是超级管理员，则只能添加自己的产品信息
            userId = getUserId();
        }

        //获取产品名称(新增.修改)
        returnData.put("inProductNameList", productOnlieInfoService.inProductNameList(userId));

        return AjaxResult.success(returnData);
    }

    /**
     * 查询产品上线信息列表
     */
    @PreAuthorize("@ss.hasPermi('product:onlieInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProductOnlieInfoParam productOnlieInfoParam)
    {
        // 查询当前

        if (!(SecurityUtils.loginUserIsAdmin() || getUserId().equals(429L))){
            // 当前登陆用户不是超级管理员，则只能添加自己的产品信息
            productOnlieInfoParam.setOwnerId(getUserId());
        }

        startPage();
        List<Map<String, Object>> list = productOnlieInfoService.selectProductOnlieInfoMapList(productOnlieInfoParam);
        list = new ProductOnlieInfoWrapper(list).wrap();
        return getDataTable(list);
    }

    /**
     * 导出产品上线信息列表
     */
    @PreAuthorize("@ss.hasPermi('product:onlieInfo:export')")
    @Log(title = "产品上线信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductOnlieInfo productOnlieInfo)
    {
        List<ProductOnlieInfo> list = productOnlieInfoService.selectProductOnlieInfoList(productOnlieInfo);
        ExcelUtil<ProductOnlieInfo> util = new ExcelUtil<ProductOnlieInfo>(ProductOnlieInfo.class);
        util.exportExcel(response, list, "产品上线信息数据");
    }

    /**
     * 获取产品上线信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('product:onlieInfo:query')")
    @GetMapping(value = "/{productId}")
    public AjaxResult getInfo(@PathVariable("productId") Long productId)
    {
        return success(productOnlieInfoService.selectProductOnlieInfoByProductId(productId));
    }

    /**
     * 新增产品上线信息
     */
    @PreAuthorize("@ss.hasPermi('product:onlieInfo:add')")
    @Log(title = "产品上线信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductOnlieInfo productOnlieInfo)
    {
        return productOnlieInfoService.insertProductOnlieInfo(productOnlieInfo);
    }

    /**
     * 修改产品上线信息
     */
    @PreAuthorize("@ss.hasPermi('product:onlieInfo:edit')")
    @Log(title = "产品上线信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductOnlieInfo productOnlieInfo)
    {
        return toAjax(productOnlieInfoService.updateProductOnlieInfo(productOnlieInfo));
    }


    /**
     * 修改产品上线信息
     */
    @PostMapping("/updateStatus")
    public AjaxResult updateStatus(@RequestBody ProductOnlieInfo productOnlieInfo)
    {
        return toAjax(productOnlieInfoService.updateStatus(productOnlieInfo));
    }

    /**
     * 修改产品跳转状态
     */
    @PostMapping("/updateJumpStatus")
    public AjaxResult updateJumpStatus(@RequestBody ProductOnlieInfo productOnlieInfo)
    {
        return toAjax(productOnlieInfoService.updateJumpStatus(productOnlieInfo));
    }

    /**
     * 删除产品上线信息
     */
    @PreAuthorize("@ss.hasPermi('product:onlieInfo:remove')")
    @Log(title = "产品上线信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{productIds}")
    public AjaxResult remove(@PathVariable Long[] productIds)
    {
        return toAjax(productOnlieInfoService.deleteProductOnlieInfoByProductIds(productIds));
    }

    /**
     * 修改产品上线状态
     */
    @PreAuthorize("@ss.hasPermi('product:onlieInfo:edit')")
    @Log(title = "修改产品上线状态", businessType = BusinessType.UPDATE)
    @PutMapping("editStatusById")
    public AjaxResult editStatusById(@RequestParam("id") Long id, @RequestParam("status") Long status)
    {
        if (id !=null && status!=null){
            return productOnlieInfoService.updateProductOnlieStatusById(id,status);
        }else {
            return AjaxResult.error("参数未传入");
        }

    }

    /**
     * 查询产品名称列表
     */
    @GetMapping("/productList")
    public AjaxResult productList()
    {

        return success(productBaseInfoService.productNameList());
    }

//    /**
//     * 一键置顶
//     * */
//    @PutMapping("oneClickToTop")
//    public AjaxResult oneClickToTop(@RequestParam("id") Long id){
//        if (id != null){
//            //通过id拿到该条对象信息，修改其排序权重9999，从新查询
//            return productOnlieInfoService.updateOneClickToTopById(id);
//        }else {
//            return AjaxResult.error("参数未传入");
//        }
//    }
//
//    /**
//     * 取消置顶
//     * */
//    @PutMapping("cancelTopping")
//    public AjaxResult cancelTopping(@RequestParam("id") Long id){
//        if (id != null){
//            //通过id拿到该条对象信息，修改其排序权重1，从新查询
//            return productOnlieInfoService.updateCancelToppingById(id);
//        }else {
//            return AjaxResult.error("参数未传入");
//        }
//    }

    @GetMapping("/sort")
    public TableDataInfo productListBySort(ProductOnlieInfo productOnlieInfo)
    {
        startPage();
        List<Map<String, Object>> list = productOnlieInfoService.productListBySort();
        list = new ProductOnlieInfoWrapper(list).wrap();
        return getDataTable(list);

    }

}
